/**
 * Licensed to Cloudera, Inc. under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  Cloudera, Inc. licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cloudera.util;

public class Pair<L, R> {
  private final L left;
  private final R right;

  final public R getRight() {
    return right;
  }

  final public L getLeft() {
    return left;
  }

  public Pair(final L left, final R right) {
    this.left = left;
    this.right = right;
  }

  @Override
  @SuppressWarnings("unchecked")
  public final boolean equals(Object o) {
    if (!(o instanceof Pair))
      return false;

    Pair<L, R> other = (Pair<L, R>) o;

    if (getLeft() != null && !getLeft().equals(other.getLeft())) {
      return false;
    } else if (getLeft() == null && other.getLeft() != null) {
      return false;
    } else if (getRight() != null && !getRight().equals(other.getRight())) {
      return false;
    } else if (getRight() == null && other.getRight() != null) {
      return false;
    } else {
      return true;
    }
  }

  @Override
  public int hashCode() {
    int hLeft = getLeft() == null ? 0 : getLeft().hashCode();
    int hRight = getRight() == null ? 0 : getRight().hashCode();

    // an arbitrary prime number
    return hLeft + (61 * hRight);
  }

  @Override
  public String toString() {
    return "<" + getLeft() + ", " + getRight() + ">";
  }

}
